﻿using UnityEngine;
using UnityEngine.UI;

[shaco.UILayerIndex((int)ShootMan.GlobalDefines.UILayers.Game)]
[shaco.UILayerOpenAsync]
public class GameNetLayer : UnityEngine.MonoBehaviour
{
    void OnUIOpen(shaco.Base.BaseEventArg arg)
    {
        this.AddAutoReleaseEvent<ShootMan.SyncTransformPositionNetEvent>((sender, arg) =>
        {
            var newEvent = shaco.GameHelper.objectpool.Instantiate<ShootMan.NetEvent.C2S_NotifySelfPosition>(() => new ShootMan.NetEvent.C2S_NotifySelfPosition());
            newEvent.playerID = ShootMan.UserData.playerID;
            newEvent.position = arg.position;
            ShootMan.NetServiceComponent.SendMessage(newEvent);
            newEvent.RecyclingWithPool();
        });

        this.AddAutoReleaseEvent<ShootMan.SyncShootBulletNetEvent>((sender, arg) =>
        {
            var newEvent = shaco.GameHelper.objectpool.Instantiate<ShootMan.NetEvent.C2S_NotifySelfShoot>(() => new ShootMan.NetEvent.C2S_NotifySelfShoot());
            newEvent.playerID = ShootMan.UserData.playerID;
            newEvent.shootCount = 1;
            ShootMan.NetServiceComponent.SendMessage(newEvent);
            newEvent.RecyclingWithPool();
        });

        ShootMan.NetServiceComponent.AddDataReceiveListener(e =>
        {
            if (e.GetType() == typeof(ShootMan.NetEvent.S2C_NotifySelfPosition))
            {
                var ee = e as ShootMan.NetEvent.S2C_NotifySelfPosition;
                var playerTmp = ShootMan.GameRoot.CheckAutoAddPlayer(ee.playerID);
                if (null == playerTmp || ShootMan.UserData.playerID == ee.playerID)
                    return;
                playerTmp.transform.position = ee.position;
            }
            else if (e.GetType() == typeof(ShootMan.NetEvent.S2C_NotifySelfShoot))
            {
                var ee = e as ShootMan.NetEvent.S2C_NotifySelfShoot;
                var playerTmp = ShootMan.GameRoot.CheckAutoAddPlayer(ee.playerID);
                if (null == playerTmp || ShootMan.UserData.playerID == ee.playerID)
                    return;
                playerTmp.Shoot();
            }
            else if (e.GetType() == typeof(ShootMan.NetEvent.S2C_NotifySpawnEnemy))
            {
                var ee = e as ShootMan.NetEvent.S2C_NotifySpawnEnemy;
                var playerTmp = ShootMan.GameRoot.CheckAutoAddPlayer(ee.playerID);
                if (null == playerTmp || ShootMan.UserData.playerID == ee.playerID)
                    return;

                //收到网络推送创建敌人
                var newEnemyGameObject = shaco.GameHelper.objectpool.InstantiateWithKey<UnityEngine.GameObject>(ee.enemyPrefabName, () =>
                {
                    return shaco.GameHelper.res.LoadEx<UnityEngine.GameObject>($"{ShootMan.GlobalDefines.HOTUPDATE_ROOT}" + ee.enemyPrefabName);
                });

                //重置重力
                var rigidbody = newEnemyGameObject.GetComponent<UnityEngine.Rigidbody>();
                if (null != rigidbody)
                {
                    var oldConstraints = rigidbody.constraints;
                    rigidbody.constraints = UnityEngine.RigidbodyConstraints.FreezeAll;
                    rigidbody.constraints = oldConstraints;
                }

                //随机一个出生点
                var gameLayer = shaco.GameHelper.ui.GetUIComponent<GameLayer>();
                var gameRoot = gameLayer.GetGameRoot();
                newEnemyGameObject.transform.position = ee.position;
                newEnemyGameObject.transform.SetParent(gameRoot.enemySpawnPoint.transform, false);
            }
        });
    }

    void OnDestory()
    {
        ShootMan.NetServiceComponent.ClearDataReceiveListener();
    }
}